//
//  BSSQualificationChooseFirstViewController.m
//  PreschoolEducation
//
//  Created by 传铎 on 2020/11/17.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSQualificationChooseFirstViewController.h"
#import "BSSPRHomePageCollectionViewCell.h"
#import "BSSServiceCategoryListViewController.h"
#import "BSSQualificationChooseSecendViewController.h"
#define CellID @"BSSPRHomePageCollectionViewCell"
@interface BSSQualificationChooseFirstViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic)  UICollectionViewFlowLayout *Collayout;
@property (strong,nonatomic)  UICollectionView *collectionView;
@property (strong, nonatomic) UIPageControl *pageControl;

@end

@implementation BSSQualificationChooseFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}
- (void)creatUI{
    self.view.backgroundColor = colorCode(@"#F9F9F9");
    UILabel *titleL = [[UILabel alloc]init];
    [self.view addSubview:titleL];
    titleL.text = @"请选择资质一级类目：";
    titleL.textColor = colorCode(@"333333");
    titleL.font = fontCount(16);
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(__kNavigationBarHeight__ + 12);
    }];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    // 水平方向滑动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(7, __kNavigationBarHeight__ + 46, SCREEN_WIDTH - 14, SCREEN_HEIGHT - __kNavigationBarHeight__ - 46) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = colorCode(@"#F9F9F9");
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    self.pageControl.numberOfPages= 3;
    
    _collectionView.alwaysBounceHorizontal = NO;
    _collectionView.contentInset = UIEdgeInsetsMake(0, -15, 0, 15);
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
    _collectionView.contentSize = CGSizeMake(0,SCREEN_HEIGHT - __kNavigationBarHeight__ - 46);
    [self.collectionView registerClass:[BSSPRHomePageCollectionViewCell class] forCellWithReuseIdentifier:CellID];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
  
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else if (section == 1) {
        return 4;
    }else{
        return 1;
    }
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    BSSPRHomePageCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSArray *nameArr = @[@[@"音乐培训",@"外语培训"],@[@"托儿所",@"舞蹈",@"画画",@"体育"]];
    NSArray *imageArr = @[@[@"ic_yinyue",@"ic_waiyu"],@[@"ic_tuoersuo",@"ic_wudao",@"ic_huahua",@"ic_yundong"]];
    cell.titileLabel.text = nameArr[indexPath.section][indexPath.row];
    cell.mainImageView.image = imageNameStr(imageArr[indexPath.section][indexPath.row]);
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(SCREEN_WIDTH, 55);
    
}
- ( UICollectionReusableView *)collectionView:( UICollectionView *)collectionView viewForSupplementaryElementOfKind:( NSString *)kind atIndexPath:( NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader ){
        
        UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        for (UIView *subView in reusableview.subviews) {
            [subView removeFromSuperview];
        }
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
        view.backgroundColor = colorCode(@"#F9F9F9");
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.backgroundColor = colorCode(@"#F34B4D");
        [view addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(15.5);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(4);
        }];
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont boldSystemFontOfSize:16];
        label.textColor = colorCode(@"333333");
        if (indexPath.section == 0) {
            label.text = @"可选择类目";
        }else{
            label.text = @"需补充资料类目";
        }
        
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageV.mas_right).offset(14);
            make.centerY.mas_equalTo(imageV.mas_centerY);
        }];
        [reusableview addSubview:view];
        return reusableview;
    }else if (kind == UICollectionElementKindSectionFooter){
        UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        for (UIView *subView in reusableview.subviews) {
            [subView removeFromSuperview];
        }
        return reusableview;
    }
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BSSQualificationChooseSecendViewController *vc = [[BSSQualificationChooseSecendViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma  mark UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake((SCREEN_WIDTH  - 14 - 20)/3.0 - 8,112);
    
}

//定义每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 8, 0, 8);//分别为上、左、下、右
}

@end
